/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pdf.java
 *
 * Created on Jan 10, 2012, 10:58:39 PM
 */

package GUI;

/**
 *
 * @author raulnavarrogomez
 */
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.swing.*;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Color;
import java.awt.Dimension;
public class pdf extends javax.swing.JFrame {

        JButton crear;
	JFileChooser selector;
	JTextArea texto;





    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public pdf(final String factura) throws IOException{
    		JPanel p=new JPanel();
		crear=new JButton("Guardar Archivo");
                crear.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				selector=new JFileChooser();
				int op=selector.showSaveDialog(pdf.this);
				if(op==JFileChooser.APPROVE_OPTION){
					try {
						OutputStream archivo=new FileOutputStream(selector.getSelectedFile());
						Document doc=new Document();
						PdfWriter.getInstance(doc, archivo);
						doc.open();
                                                doc.add(new Paragraph(factura));
						doc.close();
						archivo.close();
                                                dispose();
					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (DocumentException ex) {
						// TODO Auto-generated catch block
						ex.printStackTrace();
					} catch (IOException ex2) {
						// TODO Auto-generated catch block
						ex2.printStackTrace();
					}
				}
			}

		});
                p.add(crear);
		add(p,BorderLayout.NORTH);
		texto=new JTextArea();
                texto.setText(factura);
                texto.setEditable(false);
                add(new JScrollPane(texto));
                p.add(texto);
                p.setBackground(Color.DARK_GRAY);
    }


}
